""""
自定义分页组件，之后如果想使用这个分页组件，你需要做如下的事情：
在视图函数中：
def mobile_list(request):

    # 1. 筛选自的情况去筛选自己的数据
    queryset = models.MobileInfo.objects.filter。all()

    # 2. 实例化分页对象
    page_object = Pagination(request,queryset)


    context = {
        'queryset': page_object.page_queryset,   # 分完页的数据
        "page_string": page_object.html()   # 生成所有的页码
    }
    return render(request, 'mobile_list.html', context)

在HTML页面中进行如下编写
      // 数据的循环展示
      {% for obj in queryset %}
            {{ obj.id }}
      {% endfor %}

      // 页码的展示
      <ul class="pagination">
        {{ page_string }}
      </ul>
"""

from django.utils.safestring import mark_safe
import copy

class Pagination(object):
    def __init__(self, request, queryset, page_size=10, page_param="page", plus=5):
        """
        :param request: 请求对象
        :param queryset: 查询数据（根据此进行分页）
        :param page_size: 每页多少条数据
        :param page_param: URL 参数中的分页参数
        :param plus: 当前页前后显示几页
        """
        query_dict = copy.deepcopy(request.GET)
        query_dict.mutable = True
        self.query_dict = query_dict

        self.params = page_param
        page = request.GET.get(page_param, 1)
        if str(page).isdecimal():
            page = int(page)
        else:
            page = 1
        self.page = page
        self.page_size = page_size
        self.start = (page - 1) * page_size
        self.end = page * page_size
        self.page_queryset = queryset[self.start:min(self.end, len(queryset))]

        # 计算总页数
        total_count = queryset.count()
        self.total_page_count = (total_count + page_size - 1) // page_size
        self.plus = plus
        # # 总页码
        # total_count = queryset.count()
        # total_page_count, div = divmod(total_count, page_size)   ## divmod返回商和余数
        # if div:
        #     total_page_count += 1
        # self.total_page_count = total_page_count
        # self.plus = plus

    def html(self):
        # 计算页码区间
        if self.total_page_count <= 2 * self.plus + 1:
            start_page = 1
            end_page = self.total_page_count
        else:
            if self.page <= self.plus:
                start_page = 1
                end_page = 2 * self.plus + 1
            else:
                if (self.page + self.plus) > self.total_page_count:
                    start_page = self.total_page_count - 2 * self.plus
                    end_page = self.total_page_count
                else:
                    start_page = self.page - self.plus
                    end_page = self.page + self.plus

        # 页码生成
        page_str_list = []
        self.query_dict.setlist(self.params, [1])
        prev = f'<li><a href="?{self.query_dict.urlencode()}">首页</a></li>'
        page_str_list.append(prev)

        # 上一页
        if self.page > 1:
            self.query_dict.setlist(self.params, [self.page - 1])
            prev = f'<li><a href="?{self.query_dict.urlencode()}">上一页</a></li>'
        else:
            self.query_dict.setlist(self.params, [1])
            prev = f'<li><a href="?{self.query_dict.urlencode()}">上一页</a></li>'
        page_str_list.append(prev)

        # 页码
        for i in range(start_page, end_page + 1):
            self.query_dict.setlist(self.params, [i])
            if i == self.page:
                ele = f'<li class="active"><a href="?{self.query_dict.urlencode()}">{i}</a></li>'
            else:
                ele = f'<li><a href="?{self.query_dict.urlencode()}">{i}</a></li>'
            page_str_list.append(ele)

        # 下一页
        if self.page < self.total_page_count:
            self.query_dict.setlist(self.params, [self.page + 1])
            prev = f'<li><a href="?{self.query_dict.urlencode()}">下一页</a></li>'
        else:
            self.query_dict.setlist(self.params, [end_page])
            prev = f'<li><a href="?{self.query_dict.urlencode()}">下一页</a></li>'
        page_str_list.append(prev)

        # 尾页
        self.query_dict.setlist(self.params, [self.total_page_count])
        prev = f'<li><a href="?{self.query_dict.urlencode()}">尾页</a></li>'
        page_str_list.append(prev)

        page_string = mark_safe("".join(page_str_list))
        return page_string
